package reflection.Test;

import java.lang.reflect.Field;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-07-02 16:04
 * @version: 1.0
 */
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchFieldException {
        Class son = Class.forName("reflection.Test.Son");
        Son o = (Son) son.newInstance();
        //获取本类声明的属性
        Field[] fields = son.getDeclaredFields();
        for(Field f:fields){
            System.out.println(f);
        }
//        //破坏封装性
            Field age = son.getDeclaredField("age");
        try{
//            age.setAccessible(true);
            age.set(o, 11);
            System.out.println(o);
        } catch (Exception e) {
//            e.printStackTrace();
            System.err.println("不能使用");
        }
        System.out.println();
        //获取父类声明的属性
        Field[] fields1 = son.getFields();
        for(Field f:fields1){
            System.out.println(f);
        }
    }
}
